Repository navigation
fix(runtime): 修复 once 监听器的取消与解绑时机 - #19583
Conversation
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes.
|
| Layer / File(s) | Summary |
|---|---|
移除时机与生命周期回归验证 packages/taro-runtime/src/dom/event-target.ts, packages/taro-runtime/tests/event-once.spec.ts |
移除 once 专用包装逻辑,并在调用用户回调前移除当前注册。新增测试覆盖原回调取消、同步重入、异常、事件名规范化、regionchange 别名、重复注册及 once: false。 |
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Bug fix · Severity of issue fixed: Medium
Suggested reviewers: chen-jj, single-dancer
Merge Risk: 
Minimal · previous review 0569c
-
Once listeners can now be cancelled and are removed before invocation. No introduced merge-blocking risk was identified.
Pre-merge checks: 
5
-
Check name Status Explanation Description Check Check skipped - CodeRabbit’s high-level summary is enabled. Title check 标题准确概括了 PR 的主要变更,即修复 runtime 中 once 监听器的取消和解绑时机。标题简洁且与改动内容直接相关。 Linked Issues check [ #19582] 需求:once 监听器必须可用原始 callback 取消;必须在执行用户回调前移除当前注册,使同步重入和回调抛错都不会重复调用。
代码证据(merge base 603489c 至 head 0569c72):
1. 删除了isOnce时的额外包装和递归addEventListener。原来的 once 包装器不再套在原回调外层,因此 `rem…Out of Scope Changes check 本次 PR 的变更范围限定在 packages/taro-runtime/src/dom/event-target.ts(源码修复)和packages/taro-runtime/tests/event-once.spec.ts(新增回归测试)。
源码变更只涉及 once 监听器的包装与解绑逻辑,直接对应 [#19582] 的需求。删除的额外包装和新增的移除调用均属于同一修复。新增测试覆…Docstring Coverage No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentat… Full details: Linked Issues check
-
Explanation
[
#19582] 需求:once 监听器必须可用原始 callback 取消;必须在执行用户回调前移除当前注册,使同步重入和回调抛错都不会重复调用。代码证据(merge base 603489c 至 head 0569c72):
- 删除了
isOnce时的额外包装和递归addEventListener。原来的 once 包装器不再套在原回调外层,因此removeEventListener(type, callback)可以通过现有item.oldHandler === handler匹配,取消原回调注册。 - 统一包装器内新增
if (isOnce) this.removeEventListener(type, handler)。此处handler指向已被替换的包装器,因此移除的是当前注册。该移除发生在oldHandler.apply之前,同步重入时该注册已不存在,抛错时也已完成解绑。 regionchange仍分别调用begin和end的addEventListener,各自生成独立包装器,因此两条生命周期互不影响。once: false路径不执行移除,行为保持不变。
测试证据:PR 描述列出 12 个回归用例,覆盖原回调取消、同步重入、抛错后解绑、普通与 once 共用回调、重复注册和 regionchange。本次未逐行核对测试文件,结论依赖 diff 与 PR 描述中的测试说明。
未覆盖项:issue 不要求小程序开发者工具或真机验证,因此不属于本次判定范围。
次要行为变化:
hooks.call('onAddEvent', ...)对 once 监听器原本调用两次(原始 handler 一次,包装器一次),现在只调用一次且传入原始 handler 与{ once: true }。这是去除额外包装的直接结果。issue 未要求保留该调用次数,因此不影响本次判定。 - 删除了
Full details: Out of Scope Changes check
-
Explanation
本次 PR 的变更范围限定在
packages/taro-runtime/src/dom/event-target.ts(源码修复)和packages/taro-runtime/tests/event-once.spec.ts(新增回归测试)。源码变更只涉及 once 监听器的包装与解绑逻辑,直接对应 [
#19582] 的需求。删除的额外包装和新增的移除调用均属于同一修复。新增测试覆盖 issue 所列行为及 PR 描述中的兼容性场景(普通监听器、regionchange、once: false 对照),属于支持该修复的测试,不属于无关变更。未发现与 [
#19506](packages/shared 的 Events.once)或 [#19529](移除未注册处理器)相关的改动,两者在 diff 中均无对应代码。
Full details: Docstring Coverage
-
Explanation
No eligible functions found in the changed files. Skipping docstring coverage check. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 0 functions across 2 files.
-
Finishing Touches
-
🧪 Generate unit tests (beta)
-
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
小兔看见监听器,先解开再回声。
重入敲门时,旧回调不再应声。
错误吹过草地,注册已悄然离去。
多次注册各自守候,按次完成职责。once: false 仍听两遍,移除后归于安静。
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #19583 +/- ##
==========================================
+ Coverage 57.56% 57.57% +0.01%
==========================================
Files 470 470
Lines 25971 25966 -5
Branches 6530 6519 -11
==========================================
+ Hits 14950 14951 +1
- Misses 9124 9178 +54
+ Partials 1897 1837 -60
Flags with carried forward coverage won't be shown. Click here to find out more.
🚀 New features to boost your workflow:
|
10cb657 to
3e65bd8
Compare
这个 PR 做了什么?
runtime DOM 的 once 监听器额外包装原回调,导致
removeEventListener(type, callback)无法取消注册;回调返回后才解绑,也使同步重入和回调抛错后再次派发时重复执行。复用现有监听器包装逻辑,让
oldHandler直接指向原回调;once 在调用用户代码前通过现有removeEventListener路径移除当前包装器。保留每次注册的独立包装、普通监听器和 regionchange 的 begin/end 独立生命周期。Closes #19582
这个 PR 是什么类型?
这个 PR 涉及以下平台:
验证
pnpm --filter @tarojs/runtime run test:ci --maxWorkers=2:20 个文件通过,285 个用例通过、1 个跳过。Node.js 26.8.1 / pnpm 10.0.0。pnpm --filter @tarojs/runtime run build:Node.js 22.14.0 / pnpm 10.0.0,退出码 0。沙箱内首次构建生成产物后未退出,结束空闲进程并在放宽沙箱限制后重跑通过。git diff --check通过。分支独立基于官方 main 的
603489c8e73,仅包含 runtime 源码和新增测试两个文件。测试直接调用真实 runtime;未运行全工作区构建、小程序开发者工具或真机验证。与 #19506 的 shared
Events.once、#19529 的未注册监听器移除修复属于不同问题。Summary by CodeRabbit